﻿using KamRemakeRemake.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace KamRemakeRemake.GameObjects.MapObjects {
    public class Tile : IDrawableGameObject {
        public const int TILEWIDTH = 32;
        public const int TILEHEIGHT = 32;

        private Texture2D texture;

        public TileType TileType { get; set; }
        public Map Map { get; set; }
        public int Row { get; private set; }
        public int Column { get; private set; }

        public Tile(TileType type, int row, int column) {
            this.TileType = type;
            this.Row = row;
            this.Column = column;
        }

        /// <summary>
        /// Returns the current rectangle of the tile, this is adjusted by the camera position
        /// </summary>
        public Rectangle ScreenRectangle {
            get {
                return new Rectangle(
                    Column*TILEWIDTH - (int) Camera.Instance.Position.X,
                    Row*TILEHEIGHT - (int) Camera.Instance.Position.Y,
                    TILEWIDTH,
                    TILEHEIGHT);
            }
        }

        /// <summary>
        /// Returns the current rectangle of the tile
        /// </summary>
        public Rectangle MapRectangle {
            get {
                return new Rectangle(
                    Column*TILEWIDTH,
                    Row * TILEHEIGHT,
                    TILEWIDTH,
                    TILEHEIGHT);
            }
        }

        /// <summary>
        /// Returns wether or not a tile is passable
        /// </summary>
        public bool IsPassable() {
            return TileTypeList.Passable().Contains(this.TileType);
        }

        /// <summary>
        /// Returns the current rectangle of the tile
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, ContentManager contentManager) {
            spriteBatch.Draw(this.texture,
                             this.ScreenRectangle,
                             Color.White);
        }

        public void LoadContent(ContentManager contentManager) {
            this.texture = contentManager.Load<Texture2D>("sprites/tiles/" + TileType.ToString().ToLower());
        }

        public void Update(GameTime gameTime) {
        }
    }
}
